package com.ruoyi.common.utils.wxlogin.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpClientUtils;
import com.ruoyi.common.utils.wxlogin.NetUtil;
import com.ruoyi.common.utils.wxlogin.WeChatReCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 家长管理
 *
 * @author wuwenlong
 * date 2022年4月27日16:40:00
 */
@Controller("webParentController")
@RequestMapping("/web/parent")
public class ParentController  {

    /**
     * /web/parent/phoneNumber
     *
     * @Description: 获取手机号
     * @param code
     * @param parentId
     * @return Map<String,Object>
     * @author GSX
     * @date 2022年4月28日下午5:31:31
     * @throws
     */
    @RequestMapping(value = "phoneNumber", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> phoneNumber(String code,Long parentId) {
        Map<String, Object> map = new HashMap<String, Object>();
		String access_token = JSONObject.parseObject(WeChatReCode.access_token()).getString("access_token");

		String phoneNumberUrl="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+access_token;
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("code",code);
        String phoneNumber = null;
        try {
        	phoneNumber = HttpClientUtils.POSTRequest(phoneNumberUrl,JSONObject.toJSONString(stringStringHashMap),true);
        	System.err.println(phoneNumber);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HashMap hashMap = JSON.parseObject(phoneNumber, HashMap.class);
        if(!"0".equals(hashMap.get("errcode").toString())){
            map.put("code", 0);
            map.put("msg", hashMap.get("errmsg"));
            return map;
        }
        String phone_info = String.valueOf(hashMap.get("phone_info"));
        HashMap hashMap1 = JSON.parseObject(phone_info, HashMap.class);
        String phoneNumber1 = String.valueOf(hashMap1.get("phoneNumber"));

//        {"errcode":40029,"errmsg":"invalid code hint: [UeoBsDOre-r3tuNa] rid: 626b426c-3ba01115-69e1ddda"}
//        {"errcode":0,"errmsg":"ok","phone_info":{"phoneNumber":"15171785972","purePhoneNumber":"15171785972","countryCode":"86","watermark":{"timestamp":1651197623,"appid":"wxe66b24b0cc716cc7"}}}
        return map;
    }

    /**
     * /web/parent/wxlogin
     *
     * @Description: 微信登录
     * @param code     code
     * @return
     * @throws Exception R
     * @author GSX
     * @date 2022年4月28日上午9:37:56
     * @throws
     */
    @RequestMapping(value = "/wxlogin", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void wxlogin(String pic,String nickname,String code) throws Exception{
//    	nickname = new String(nickname.getBytes("gb2312"), "utf-8");
        String appid = "wxe66b24b0cc716cc7";
        String secret = "ea6591b1d8de16338a184609ab7df66e";
        System.out.println("appid = " + appid);
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid="
                + appid + "&secret=" + secret + "&js_code=" + code
                + "&grant_type=authorization_code";
        String oppid = new NetUtil().get(requestUrl);
        JSONObject oppidObj = (JSONObject) JSONObject.parse(oppid);

        String openId = (String) oppidObj.get("openid");
        String session_key = oppidObj.get("session_key").toString();
    }
}
